package com.example.hyznbk.view.adapter;

import android.animation.Animator;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.example.hyznbk.R;
import com.example.hyznbk.model.result.BaseReviewItemEntity;
import com.example.hyznbk.utils.AnimatorHelper;
import com.example.hyznbk.view.widget.RatingBar;
import com.example.hyznbk.view.widget.RippleView;
import com.lancoo.reviewsystem.utils.TimeUtils;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



/**
 * Created by tangminglong on 17/7/25.
 * 基本复习的item适配器
 */

public class GuessSubjectAdapter extends BaseRecyclerAdapter<GuessSubjectAdapter.ViewHolder>{
    private Context mContext;
    private List<BaseReviewItemEntity.BaseReviewItemBean> mList;
    private OnItemClickListener onItemClickListener;

    private int mLastPosition = -1;

    public GuessSubjectAdapter(Context context) {
        this.mContext = context;
        mList = new ArrayList<>();
    }

    public GuessSubjectAdapter(Context mContext,  List<BaseReviewItemEntity.BaseReviewItemBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public  interface OnItemClickListener{
        void onItemClick(View view, List<BaseReviewItemEntity.BaseReviewItemBean> list,int position);

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder getViewHolder(View view) {

        return new ViewHolder(view,false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_guess_testing, parent,false);
        return new ViewHolder(view,true);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder,  int position, boolean isItem) {

        holder.rippleLayout.setTag(position);
        //设置波纹效果
        holder.rippleLayout.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView v) {
                if (onItemClickListener != null){
                    onItemClickListener.onItemClick(v,mList, (Integer) v.getTag());
                }

            }
        });

       /* holder.paperInto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if (onItemClickListener != null){
                  onItemClickListener.onItemClick(v,mList, (Integer) v.getTag());
              }
            }
        });*/

        BaseReviewItemEntity.BaseReviewItemBean itemBean = mList.get(position);

        Date date = itemBean.getLastAnsweredTime();
        String answerTime = TimeUtils.getDate(date);
        if (itemBean.getState() == 2){//已经提交
            Drawable drawRight = ContextCompat.getDrawable(mContext,R.mipmap.ic_into_result);
            drawRight.setBounds(0,0,drawRight.getIntrinsicWidth(),drawRight.getIntrinsicHeight());
            holder.tvIntoDetail.setText("查看结果");
            holder.tvIntoDetail.setCompoundDrawables(null,null,drawRight,null);
        }else if (itemBean.getState() == 1){
            Drawable drawRight = ContextCompat.getDrawable(mContext,R.mipmap.ic_into_answer);
            drawRight.setBounds(0,0,drawRight.getIntrinsicWidth(),drawRight.getIntrinsicHeight());
            holder.tvIntoDetail.setText("继续作答");
            holder.tvIntoDetail.setCompoundDrawables(null,null,drawRight,null);
        }else if (itemBean.getState() == 0){
            Drawable drawRight = ContextCompat.getDrawable(mContext,R.mipmap.ic_into_answer);
            drawRight.setBounds(0,0,drawRight.getIntrinsicWidth(),drawRight.getIntrinsicHeight());
            holder.tvIntoDetail.setText("开始作答");
            holder.tvIntoDetail.setCompoundDrawables(null,null,drawRight,null);
            answerTime = mContext.getString(R.string.no_answered);
        }
        String lastAnswerTime = String.format(mContext.getString(R.string.paper_lastAnswerTime), answerTime);
        int finishedCount = itemBean.getAnsweredItemCount();
        int totalCount = itemBean.getTotalItemCount();

        String[] HHmmss = TimeUtils.getHHmmssFromSeconds(itemBean.getDuration());
        String paperUsedTime = String.format(mContext.getString(R.string.paper_usedTime),
                HHmmss[0],HHmmss[1],HHmmss[2]);
        holder.paperInduction.setText(itemBean.getTestPaperName());
        holder.tvAnswerUsed.setText(Html.fromHtml(paperUsedTime));
        holder.paperLastAnswerTime.setText(lastAnswerTime);
        holder.ratingBar.setStar(itemBean.getDifficulty());

        float finishedPercent = (float) Math.floor((float)finishedCount / totalCount * 100) / 100;
        //float finishedPercent = (float)finishedCount / totalCount;
        NumberFormat format = NumberFormat.getPercentInstance();// 获取格式化类实例 带百分比
        format.setMinimumFractionDigits(0);// 设置小数位
        holder.paperFinishPercent.setText("作答"+ format.format(finishedPercent));




        holder.progressBar.setProgress((int)(finishedPercent*100));


        int adapterPosition = holder.getAdapterPosition();

        boolean isScrollDown = adapterPosition > mLastPosition;

        for (Animator anim : AnimatorHelper.getRecyclerAnimator(holder.itemView,isScrollDown)) {
            anim.start();
        }
        mLastPosition = adapterPosition;
    }

    @Override
    public int getAdapterItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView paperLastAnswerTime;
        private RippleView rippleLayout;
        private TextView paperInduction;//资料简介
        private TextView paperFinishPercent;//资料完成度
        private ProgressBar progressBar;//资料进度显示
        private LinearLayout paperInto;
        private RatingBar ratingBar;
        private TextView tvAnswerUsed;
        private TextView tvIntoDetail;

        public ViewHolder(View itemView,boolean isItem) {
            super(itemView);
            if (isItem){
                //波纹效果
                rippleLayout = (RippleView) itemView.findViewById(R.id.item_ripper);
                paperInduction = (TextView) itemView.findViewById(R.id.tv_induction);
                paperLastAnswerTime = (TextView) itemView.findViewById(R.id.tv_lastTime);
                tvAnswerUsed = (TextView) itemView.findViewById(R.id.tv_answerUsed);
                paperFinishPercent = (TextView) itemView.findViewById(R.id.tv_finishedPercent);
                progressBar = (ProgressBar) itemView.findViewById(R.id.pb_finished);
                paperInto = (LinearLayout) itemView.findViewById(R.id.liner_into);
                ratingBar = (RatingBar) itemView.findViewById(R.id.rb_hval);
                tvIntoDetail = (TextView) itemView.findViewById(R.id.tv_into_detail);
            }
        }
    }

    public void add(List<BaseReviewItemEntity.BaseReviewItemBean> data){
        int startPosition = getStart()+getAdapterItemCount();
        mList.addAll(data);
        notifyItemRangeInserted(startPosition,data.size());

    }

    public void clearAndAdd(List<BaseReviewItemEntity.BaseReviewItemBean> data){
        mLastPosition = -1;
        mList.clear();
        mList.addAll(data);
        notifyDataSetChanged();
    }
}
